// pages/bookHotel/bookHotel.js
let app=getApp();
var roomPrice;
var roomTypeName;
var roomName;
var startDate;
var endDate;
var total;//不变
var dayCount;
var totalRooms;
var roomNum;
var names;
var liveNum;
var arriveTime;
var selectTime;
var nameList;
var phone;
var remark;
var typeId;
var arriveDate;
var selectCoupon;
var allCoupon;
var finalPrice;
function Coupon(){
     var id;
     var money;
}
var Time=['14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00','00:00','01:00','02:00','03:00','04:00','05:00','06:00'];
var util = require('../../utils/util.js');
Page({

     /**
      * 页面的初始数据
      */
     data: {
          isDiscount: false,
          roomPrice,
          roomTypeName,
          roomName,
          startDate,
          endDate,
          total,//费用
          dayCount,//居住天数
          roomShow:false,
          timeShow:false,
          couponShow:false,
          totalRooms,//剩余房间
          roomNum:1,//选择的房间数量
          names:[{index:"",name:""}],//要填写的入住人姓名列表
          liveNum,//每间房可住人数
          arriveTime:[],//可选择的预计到达时间
          arriveDate,
          selectTime,//选择的预计到达时间
          nameList:[],//入住人姓名列表
          phone,//获取绑定的信息里的手机号
          remark:'',//备注
          typeId:0,//房间类型ID,据此获取剩余房间
          allCoupon:[],//可选的优惠券
          selectCoupon//选择的优惠券
     },
     //显示隐藏房间数量
     showButton:function(){
          this.setData({
               roomShow:!this.data.roomShow
          })
     },
     //选择房间数量
     roomNumSelect:function(e){
          //获取选择的房间数量
          roomNum=e.target.dataset.num;
          //计算总价
          total=parseInt(roomNum)*roomPrice*dayCount;
          names=new Array();
          nameList=this.data.nameList;
          for(var i=0;i<roomNum;i++){
               names.push(i+1);
          }
          //去掉多余的居住人
          for(let i=nameList.length-1; i>=0; i--){
               if(nameList[i].id>roomNum){
                    nameList.splice(i,1);
               }
          }
          if(nameList.length!=0){
               this.setData({
                    nameList:nameList
               })
          }
          this.setData({
               roomNum:roomNum,
               total:total,
               names:names
          })
     },
     //显示隐藏预计到达时间
     showTimeButton:function(){
          this.setData({
               timeShow:!this.data.timeShow
          })
     },
     //选择预计到达时间
     arriveTimeSelect:function(e){
          selectTime=e.target.dataset.time;
          var date = startDate;
          var a=selectTime.split(':');
          if(a[0]=='00'||a[0]=='01'||a[0]=='02'||a[0]=='03'||a[0]=='04'||a[0]=='05'||a[0]=='06'){
               date = new Date(startDate);
               date = util.transTime(date.getTime() +  1000 * 60 * 60 * 24);
          }
          arriveDate=date+" "+a[0]+":00:00";
          this.setData({
               selectTime:selectTime,
               arriveDate:arriveDate
          })
     },
     //填写入住人姓名和电话
     inputNP:function(e){
          nameList=e.detail.nameList;
          phone=e.detail.phone;
          this.setData({
               nameList:nameList
          });
          if(phone!=null){
               this.setData({
                    phone:phone
               });
          }
     },
     //显示隐藏优惠券
     couponShow:function(){
          this.setData({
               couponShow:!this.data.couponShow
          })
     },
     //选择优惠券
     selectCoupon:function(e){
          selectCoupon=e.target.dataset.coupon;
          if(selectCoupon.money!='不使用'){
               if(total>=selectCoupon.money){
                    finalPrice=total-selectCoupon.money;
                    this.setData({
                         total:finalPrice
                    });
               }
               else{
                    finalPrice=0;
                    this.setData({
                         total:finalPrice
                    })
               }
          }
          else{
               finalPrice=total;
               this.setData({
                    total:total
               })
          }
          this.setData({
               selectCoupon:selectCoupon
          });
     },
     //填写备注
     inputRemark:function(e){
          remark=e.detail.value;
          this.setData({
               remark:remark
          })
     },
     //支付
     pay:function(){
          nameList=this.data.nameList;
          phone=this.data.phone;
          var flag=0;
          var that=this;
          for(var item in nameList){
               if(nameList[item].name==''){flag=1;break;}
          }
          if(nameList.length<this.data.roomNum||flag==1){
               wx.showToast({
                    title: '请填写入住人姓名',
                    icon: 'none',
                    duration: 2000     
               })
          }
          else if(phone==''||phone==null){
               wx.showToast({
                    title: '请填写联系人电话',
                    icon: 'none',
                    duration: 2000     
               })              
          }
          else{
               var roomTypeId=this.data.typeId;
               var arriveDate=this.data.arriveDate;
               var leaveDate=this.data.endDate+" 12:00:00";
               var remark=this.data.remark;
               if(remark==''){
                    remark=null;
               }
               var roomCount=this.data.roomNum;
               var nameList=this.data.nameList;
               var couponId=this.data.selectCoupon.id;
               if(couponId==0){
                    couponId=null;
               }
               //提交订单到后台
               wx.request({
                    url:"http://101.37.14.167:8001/BookOrder/bookRoom",
                    method:"POST",
                    header:{'content-type':'application/json'},
                    data:{
                         'accountId':app.globalData.account.id,
                         'phone':phone,
                         'roomTypeId':roomTypeId,
                         'arriveDate':arriveDate,
                         'leaveDate':leaveDate,
                         'remark':remark,
                         'roomCount':roomCount,
                         'customerList':nameList,
                         'originalPrice':total,
                         'finalPrice':finalPrice,
                         'couponId':couponId
                    },
                    success: function(res){//若成功，跳转到订单页面
                         wx.showToast({
                              title:'支付成功',
                              duration:1000,
                              success:function(){
                                   setTimeout(function(){
                                        wx.redirectTo({
                                             url: '../orderList/orderList?type=all',
                                        });
                                   },1000)
                              }
                         });
                         console.log(res.data);
                    },
                    fail:function(res){
                         console.log("失败"+res.data);
                    }
               })
          }
     },
     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          var that=this;
          phone=app.globalData.account.mobile;
          roomPrice = options.price;
          roomTypeName = options.roomTypeName;
          roomName = options.roomName;
          startDate = options.startDate;
          endDate = options.endDate;
          dayCount=options.dayCount;
          typeId=parseInt(options.typeId);
          liveNum=options.liveNum;
          total=parseInt(dayCount)*parseFloat(roomPrice);
          finalPrice=total;
          selectCoupon={id:0,money:'不使用'};
          allCoupon=[];
          names=new Array();
          names.push(1);

          //获取剩余房间数据
          wx.request({
               url:"http://101.37.14.167:8001/Room/findByRoomTypeAndStatus",
               method:"POST",
               header:{'content-type':'application/x-www-form-urlencoded'},
               data:{
                    id:typeId,
                    status:0
               },
               success: function(res){
                    var list=res.data.data;
                    totalRooms=[];
                    if(list.length!=0){
                         for(var i=0;i<list.length;i++){
                              totalRooms.push(i+1);
                         }
                    }
                    that.setData({
                         totalRooms:totalRooms
                    });
               },
               fail:function(res){
                    console.log("失败"+res.data);
                    wx.showToast({
                         title:'请求失败',
                         icon:'none',
                         duration:2000
                    });
               }
           })

          //获取优惠券数据
          wx.request({
               url:"http://101.37.14.167:8001/Coupon/findCouponByAccount_WeChat_OpenidAndStatus",
               method:"POST",
               header:{'content-type':'application/json'},
               data:{
                    'openid':app.globalData.openId,//根据openId获取优惠券
                    'status':0
               },
               success: function(res){
                    var list=res.data.data;
                    var coupon=new Coupon();
                    coupon.id=0;
                    coupon.money='不使用';
                    allCoupon.push(coupon);
                    if(list.length!=0){
                         for(var i=0;i<list.length;i++){
                              var coupon=new Coupon();
                              coupon.id=list[i].id;
                              coupon.money=list[i].money;
                              allCoupon.push(coupon);
                         }
                    }
                    that.setData({
                         allCoupon:allCoupon
                    });
               },
               fail:function(res){
                    console.log("失败"+res.data);
                    wx.showToast({
                         title:'请求失败',
                         icon:'none',
                         duration:2000
                    });
               }
          })
          

          //将当前时间与可选的预计到达时间进行比较
          arriveTime=new Array();
          for(var time in Time){
               var date = startDate;
               var a=Time[time].split(':');
               if(a[0]=='00'||a[0]=='01'||a[0]=='02'||a[0]=='03'||a[0]=='04'||a[0]=='05'||a[0]=='06'){
                    date = new Date(startDate);
                    date = util.transTime(date.getTime() +  1000 * 60 * 60 * 24);
               }
               var b=date+" "+Time[time]+":00";
               if(new Date(b).getTime()>new Date().getTime()){
                    arriveTime.push(Time[time]);
               }
          }
          arriveDate=startDate+" "+arriveTime[0]+":00";

          this.setData({
               roomPrice: roomPrice,
               roomTypeName: roomTypeName,
               roomName: roomName,
               startDate: startDate,
               endDate: endDate,
               dayCount:dayCount,
               total:total,
               liveNum:liveNum,
               arriveTime:arriveTime,
               selectTime:arriveTime[0],
               typeId:typeId,
               arriveDate:arriveDate,
               selectCoupon:selectCoupon,
               allCoupon:allCoupon,
               names:names,
               phone:phone
          });
     },

     /**
      * 生命周期函数--监听页面初次渲染完成
      */
     onReady: function () {

     },

     /**
      * 生命周期函数--监听页面显示
      */
     onShow: function () {

     },

     /**
      * 生命周期函数--监听页面隐藏
      */
     onHide: function () {

     },

     /**
      * 生命周期函数--监听页面卸载
      */
     onUnload: function () {

     },

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function () {

     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function () {

     }
})